package cn.alterzhang.www.test.javabasic;

/**
 *
 * @author zhangshuhan
 * @date 2019/2/21
 */
public class RearrangeIn {


   static int a = 0;
   static boolean flag = false;

    public static void writer() {
        System.out.println("开始写操作 a=" + a + ",flag=" + flag);
        //1
        a = 1;
        //2
        flag = true;
    }
    public static void reader() throws InterruptedException {
        //3
        if (flag) {
            //4
            int i = a * a;
            System.out.println("i=" + i);
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Thread a = new Thread(new Runnable() {
            @Override
            public void run() {
               writer();
            }
        });

        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    reader();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        a.start();
        b.start();
        b.join();
        a.join();
    }
}
